package com.yanqu.road.dao.impl.activity.gourdplant;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.activity.gourdplant.GourdPlantCombInfo;
import com.yanqu.road.entity.activity.gourdplant.GourdPlantElement;
import com.yanqu.road.entity.activity.gourdplant.GourdPlantItemInfo;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class GourdPlantConfigDaoImpl extends TempDao {

    private static String getGourdPlantCombInfoSql = "select * from `t_s_activity_gourd_plant_comb` where `activityId`=? ORDER BY `combId` ASC";
    public List<GourdPlantCombInfo> getGourdPlantCombInfoList(int activityId) {
        List<GourdPlantCombInfo> list = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getGourdPlantCombInfoSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                GourdPlantCombInfo data = new GourdPlantCombInfo();
                data.setActivityId(rs.getInt("activityId"));
                data.setCombId(rs.getInt("combId"));
                data.setName(rs.getString("name"));
                data.setIcon(rs.getString("icon"));
                data.setDesc(rs.getString("desc"));
                data.setQuality(rs.getInt("quality"));
                data.setWeight(rs.getInt("weight"));
                data.setIntegral(rs.getInt("integral"));
                String str = rs.getString("rewards");
                if(!StringUtils.isNullOrEmpty(str)){
                    data.setRewards(str);
                }
                str = rs.getString("element");
                if(!"-1".equals(str)){
                    data.setElement(StringUtils.stringToIntegerList(str, "\\|"));
                }
                str = rs.getString("specialElement");
                if(!"-1".equals(str)){
                    List<String> itemStr = StringUtils.stringToStringList(str, "\\|");
                    for(String s : itemStr){
                        List<Integer> items = StringUtils.stringToIntegerList(s, "=");
                        GourdPlantElement element = new GourdPlantElement(items.get(0), items.get(1));
                        data.getSpecialElement().add(element);
                    }
                }
                data.setSort(rs.getInt("sort"));
                list.add(data);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

    private static String getGourdPlantItemInfoSql = "select * from `t_s_activity_gourd_plant_item` where `activityId`=? ORDER BY `id` ASC";
    public List<GourdPlantItemInfo> getGourdPlantItemInfoList(int activityId) {
        List<GourdPlantItemInfo> list = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getGourdPlantItemInfoSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                GourdPlantItemInfo data = new GourdPlantItemInfo();
                data.setActivityId(rs.getInt("activityId"));
                data.setId(rs.getInt("id"));
                data.setName(rs.getString("name"));
                data.setIcon(rs.getString("icon"));
                data.setDesc(rs.getString("desc"));
                list.add(data);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

}
